package com.cj.peiwan.server.other;

public class Validator {

    public static void shouldNotNull(Object param) throws InvalidParamException {
        if (param == null) {
            throw new InvalidParamException();
        }
    }

    public static void shouldNotEqual(Object param, Object other) throws InvalidParamException {
        shouldNotNull(param);
        shouldNotNull(other);
        if (param.equals(other)) {
            throw new InvalidParamException();
        }
    }

    public static void shouldEqual(Object param, Object other) throws InvalidParamException {
        shouldNotNull(param);
        shouldNotNull(other);
        if (!param.equals(other)) {
            throw new InvalidParamException();
        }
    }
    public static void valueMax(Double param, Double max) throws InvalidParamException {
        shouldNotNull(param);
        shouldNotNull(max);
        if (param.doubleValue() > max.doubleValue()) {
            throw new InvalidParamException();
        }
    }

    public static void valueMin(Double param, Double min) throws InvalidParamException {
        shouldNotNull(param);
        shouldNotNull(min);
        if (param.doubleValue() < min.doubleValue()) {
            throw new InvalidParamException();
        }
    }

    public static void valueBetween(Double param, Double min, Double max) throws InvalidParamException {
        valueMin(param, min);
        valueMax(param, max);
    }

    public static void valueMax(Integer param, Integer max) throws InvalidParamException {
        shouldNotNull(param);
        shouldNotNull(max);
        if (param.intValue() > max.intValue()) {
            throw new InvalidParamException();
        }
    }
    public static void valueMin(Integer param, Integer min) throws InvalidParamException {
        shouldNotNull(param);
        shouldNotNull(min);
        if (param.intValue() < min.intValue()) {
            throw new InvalidParamException();
        }
    }
    public static void valueBetween(Integer param, Integer min, Integer max) throws InvalidParamException {
        valueMin(param, min);
        valueMax(param, max);
    }
    public static void valueMin(Long param, Long min) throws InvalidParamException {
        shouldNotNull(param);
        shouldNotNull(min);
        if (param.longValue() < min.longValue()) {
            throw new InvalidParamException();
        }
    }

    public static void lenghthMax(String s ,Integer max) throws InvalidParamException{
        shouldNotNull(s);
        shouldNotNull(max);
        if (s.length()>max){
            throw new InvalidParamException();
        }
    }

    public static class InvalidParamException extends Exception {
        private int status ;
        public InvalidParamException(int status) {
            this.status=status;
        }
        public InvalidParamException() {
        }

        public int getStatus() {
            return status;
        }
    }
}
